using System;
using System.Data;
using System.Configuration;
using System.Web;

namespace MeetingRoom.Domain
{
    /// <summary>
    /// Summary description for Duration
    /// </summary>
    public class Duration
    {
        protected DateTime begin;

        protected DateTime end;

        public DateTime Begin
        {
            get
            {
                return begin;
            }
        }

        public DateTime End
        {
            get
            {
                return end;
            }
        }

        public Duration(DateTime begin, DateTime end)
        {
            if (begin.CompareTo(end) > 0)
            {
                throw new ArgumentException("Begin time should be early than end time");
            }
            this.begin = begin;
            this.end = end;
        }

        public bool Intersect(Duration d1, Duration d2)
        {
            return d1.Begin.CompareTo(d2.End) <= 0 && d1.End.CompareTo(d2.Begin) >= 0;
        }

        public bool Intersect(Duration anotherDuration)
        {
            if (this.Equals(anotherDuration))
            {
                return true;
            }

            if (Intersect(this, anotherDuration) || Intersect(anotherDuration, this))
            {
                return true;
            }

            return false;
        }

        public bool Equals(object obj)
        {
            if (!(obj is Duration))
            {
                return false;
            }

            Duration duration = (Duration)obj;
            return this.begin.Equals(duration.Begin) && this.end.Equals(duration.End);
        }
    }
}